<template>
  <div class="interview-schedule-container">
    <div class="page-header">
      <div class="header-content">
        <div class="header-left">
          <h1 class="page-title">面试安排</h1>
          <div class="page-subtitle">日历视图查看和管理面试时间安排</div>
        </div>
        <div class="header-actions">
          <el-button @click="handleCreateInterview" type="primary">
            <el-icon><Plus /></el-icon>
            新建面试
          </el-button>
          <el-button @click="$router.push('/interviews/applications')">
            <el-icon><Document /></el-icon>
            申请管理
          </el-button>
        </div>
      </div>
    </div>

    <div class="page-content">
      <!-- 统计卡片 -->
      <div class="stats-section">
        <el-row :gutter="24">
          <el-col :xl="6" :lg="8" :md="12" :sm="24" :xs="24">
            <el-card class="stats-card" shadow="never">
              <div class="stats-content">
                <div class="stats-icon scheduled">
                  <el-icon><Clock /></el-icon>
                </div>
                <div class="stats-info">
                  <div class="stats-value">{{ statistics.scheduled_count }}</div>
                  <div class="stats-label">本月已安排</div>
                </div>
              </div>
            </el-card>
          </el-col>

          <el-col :xl="6" :lg="8" :md="12" :sm="24" :xs="24">
            <el-card class="stats-card" shadow="never">
              <div class="stats-content">
                <div class="stats-icon in-progress">
                  <el-icon><VideoPlay /></el-icon>
                </div>
                <div class="stats-info">
                  <div class="stats-value">{{ statistics.in_progress_count }}</div>
                  <div class="stats-label">进行中</div>
                </div>
              </div>
            </el-card>
          </el-col>

          <el-col :xl="6" :lg="8" :md="12" :sm="24" :xs="24">
            <el-card class="stats-card" shadow="never">
              <div class="stats-content">
                <div class="stats-icon completed">
                  <el-icon><CircleCheck /></el-icon>
                </div>
                <div class="stats-info">
                  <div class="stats-value">{{ statistics.completed_count }}</div>
                  <div class="stats-label">本月完成</div>
                </div>
              </div>
            </el-card>
          </el-col>

          <el-col :xl="6" :lg="8" :md="12" :sm="24" :xs="24">
            <el-card class="stats-card" shadow="never">
              <div class="stats-content">
                <div class="stats-icon total">
                  <el-icon><TrendCharts /></el-icon>
                </div>
                <div class="stats-info">
                  <div class="stats-value">{{ statistics.total_count }}</div>
                  <div class="stats-label">总计</div>
                </div>
              </div>
            </el-card>
          </el-col>
        </el-row>
      </div>

      <!-- 日历组件 -->
      <div class="calendar-section">
        <el-card shadow="never">
          <InterviewCalendar
            :interviews="interviews"
            :loading="loading"
            @refresh="fetchInterviews"
            @interview-click="handleInterviewClick"
            @month-change="handleMonthChange"
          />
        </el-card>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, computed, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import {
  Plus, Document, Clock, VideoPlay, CircleCheck, TrendCharts
} from '@element-plus/icons-vue'
import InterviewCalendar from '@/components/InterviewCalendar.vue'
import { interviewApi } from '@/api/interview'

const router = useRouter()

// 响应式数据
const loading = ref(false)
const interviews = ref([])
const currentMonth = ref(new Date())

// 统计数据
const statistics = reactive({
  scheduled_count: 0,
  in_progress_count: 0,
  completed_count: 0,
  total_count: 0
})

// 计算属性
const monthStartDate = computed(() => {
  const date = new Date(currentMonth.value)
  date.setDate(1)
  return date.toISOString().split('T')[0]
})

const monthEndDate = computed(() => {
  const date = new Date(currentMonth.value)
  date.setMonth(date.getMonth() + 1, 0)
  return date.toISOString().split('T')[0]
})

// 方法
const fetchInterviews = async () => {
  loading.value = true
  try {
    const params = {
      start_date: monthStartDate.value,
      end_date: monthEndDate.value
    }

    const response = await interviewApi.getInterviewSchedule(params)
    interviews.value = response.data || []

    // 计算统计数据
    updateStatistics()
  } catch (error) {
    console.error('获取面试安排失败:', error)
    ElMessage.error('获取面试安排失败')
    interviews.value = []
  } finally {
    loading.value = false
  }
}

const updateStatistics = () => {
  const currentMonthInterviews = interviews.value.filter(interview => {
    const interviewDate = new Date(interview.scheduled_time)
    return interviewDate.getMonth() === currentMonth.value.getMonth() &&
           interviewDate.getFullYear() === currentMonth.value.getFullYear()
  })

  statistics.scheduled_count = currentMonthInterviews.filter(i => i.status === 'scheduled').length
  statistics.in_progress_count = currentMonthInterviews.filter(i => i.status === 'in_progress').length
  statistics.completed_count = currentMonthInterviews.filter(i => i.status === 'completed').length
  statistics.total_count = currentMonthInterviews.length
}

const handleMonthChange = (newMonth) => {
  currentMonth.value = newMonth
  fetchInterviews()
}

const handleInterviewClick = (interview) => {
  router.push(`/interviews/detail/${interview.id}`)
}

const handleCreateInterview = () => {
  ElMessage.info('新建面试功能开发中，请通过申请管理创建面试')
  router.push('/interviews/applications')
}

// 生命周期
onMounted(() => {
  fetchInterviews()
})
</script>

<style scoped>
.interview-schedule-container {
  min-height: 100vh;
  background: var(--el-bg-color-page);
  display: flex;
  flex-direction: column;
  transition: background-color 0.3s ease;
}

.page-header {
  background: var(--el-bg-color);
  border-bottom: 1px solid var(--el-border-color);
  padding: 24px 32px;
  position: sticky;
  top: 0;
  z-index: 100;
  box-shadow: 0 1px 3px var(--el-box-shadow-light);
  transition: all 0.3s ease;
}

.header-content {
  max-width: 1400px;
  margin: 0 auto;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.header-left {
  flex: 1;
}

.page-title {
  font-size: 28px;
  font-weight: 700;
  color: var(--el-text-color-primary);
  margin: 0 0 4px 0;
  line-height: 1.2;
}

.page-subtitle {
  font-size: 14px;
  color: var(--el-text-color-secondary);
  margin: 0;
}

.header-actions {
  display: flex;
  gap: 12px;
}

.page-content {
  flex: 1;
  padding: 24px 32px;
  margin: 0 auto;
  width: 100%;
  box-sizing: border-box;
}

.stats-section {
  margin-bottom: 24px;
}

.stats-card {
  border: 1px solid var(--el-border-color);
  border-radius: 8px;
  background: var(--el-bg-color);
  transition: all 0.3s ease;
}

.stats-card:hover {
  box-shadow: 0 4px 12px var(--el-box-shadow);
}

.stats-content {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 8px;
}

.stats-icon {
  width: 48px;
  height: 48px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
  color: white;
}

.stats-icon.scheduled {
  background: linear-gradient(135deg, #E6A23C, #F7BA2A);
}

.stats-icon.in-progress {
  background: linear-gradient(135deg, #409EFF, #66B1FF);
}

.stats-icon.completed {
  background: linear-gradient(135deg, #67C23A, #85CE61);
}

.stats-icon.total {
  background: linear-gradient(135deg, #909399, #B1B3B8);
}

.stats-info {
  flex: 1;
}

.stats-value {
  font-size: 24px;
  font-weight: 700;
  color: var(--el-text-color-primary);
  line-height: 1;
}

.stats-label {
  font-size: 14px;
  color: var(--el-text-color-secondary);
  margin-top: 4px;
}

.calendar-section .el-card {
  border: 1px solid var(--el-border-color);
  border-radius: 8px;
  background: var(--el-bg-color);
  transition: all 0.3s ease;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .page-header {
    padding: 16px 20px;
  }

  .header-content {
    flex-direction: column;
    gap: 16px;
    align-items: flex-start;
  }

  .page-title {
    font-size: 24px;
  }

  .page-content {
    padding: 16px 20px;
  }

  .stats-content {
    padding: 12px;
  }

  .stats-icon {
    width: 40px;
    height: 40px;
    font-size: 20px;
  }

  .stats-value {
    font-size: 20px;
  }
}
</style>
